{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "a53fc9ae-5467-42ba-a619-86d8095de3f0"
   },
   "source": [
    "## 导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "uuid": "d1f68a4a-0df4-44b3-8096-c2fc724b76ab"
   },
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "12da870f-8941-403b-8b89-31829b467c76"
   },
   "source": [
    "## 读取低清图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "uuid": "295cc336-d9ae-40c9-8573-d8bb0149e9b6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(270, 480, 3)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path = \"./l/Youku_00000_l/001.bmp\"\n",
    "img_l = cv2.imread(path)\n",
    "img_l.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "53ef2bd2-4cd9-44a8-ab7b-7a58e010f415"
   },
   "source": [
    "## 读取高清图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "uuid": "6df314d8-1a2f-4985-80ed-f42841b73911"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1080, 1920, 3)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path = \"./h_GT/Youku_00000_h_GT/001.bmp\"\n",
    "img_GT = cv2.imread(path)\n",
    "img_GT.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "1beb1784-3d96-4860-9e1c-2e513d97f0ec"
   },
   "source": [
    "## 插值算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "uuid": "0b06282e-39ec-4104-8281-32b54bf360bf"
   },
   "outputs": [],
   "source": [
    "size_l = (480, 270)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "uuid": "002dcf7b-2719-4263-b7e6-6eebf659f5ad"
   },
   "outputs": [],
   "source": [
    "size = (1920, 1080)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "72c23c94-d1c5-44d3-8cab-29cdcf9dc7e7"
   },
   "source": [
    "## INTER_NEAREST - 最近邻插值法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "uuid": "4a6c8fa3-01b3-4896-95d5-52266a98ed9e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_img = cv2.resize(img_l, size, interpolation=cv2.INTER_NEAREST)\n",
    "cv2.imwrite(\"./l/Youku_00000_l/001_INTER_NEAREST.bmp\", new_img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "1f9368d8-1620-4416-9166-27cc13d127e7"
   },
   "source": [
    "## INTER_LINEAR - 双线性插值法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "uuid": "a52997f6-648c-40c8-8640-68fd3168fd00"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_img = cv2.resize(img_l, size, interpolation=cv2.INTER_LINEAR)\n",
    "cv2.imwrite(\"./l/Youku_00000_l/001_INTER_LINEAR.bmp\", new_img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "d9e073b2-d9dc-41ee-bd52-0b9c913adfdd"
   },
   "source": [
    "## INTER_AREA - 基于局部像素的重采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "uuid": "1534265e-3bf5-4d1b-8150-1d14612b7b9b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_img = cv2.resize(img_l, size, interpolation=cv2.INTER_AREA)\n",
    "cv2.imwrite(\"./l/Youku_00000_l/001_INTER_AREA.bmp\", new_img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "23277bb0-354e-4beb-b817-3e0e4dd44c2f"
   },
   "source": [
    "## INTER_CUBIC - 基于4x4像素邻域的3次插值法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "uuid": "0e2931f3-ab28-48bd-99e8-5b2692f70da6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_img = cv2.resize(img_l, size, interpolation=cv2.INTER_CUBIC)\n",
    "cv2.imwrite(\"./l/Youku_00000_l/001_INTER_CUBIC.bmp\", new_img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "ce827374-8481-4330-834a-378b2f39df0d"
   },
   "source": [
    "## INTER_LANCZOS4 - 基于8x8像素邻域的Lanczos插值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "uuid": "c281d8eb-ff7f-4dc7-8b18-37404dab5a65"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_img = cv2.resize(img_l, size, interpolation=cv2.INTER_LANCZOS4)\n",
    "cv2.imwrite(\"./l/Youku_00000_l/001_INTER_LANCZOS4.bmp\", new_img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "8ff1eddb-f449-4cdf-85eb-d9ad3afce44d"
   },
   "source": [
    "## BiCubic插值算法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "10775e10-288f-4c22-9cf6-9d5bfcc22b5f"
   },
   "source": [
    "### 基函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "uuid": "f5c1c978-9bd5-45ec-97ce-dad6317841c7"
   },
   "outputs": [],
   "source": [
    "def base_function(x, a=-0.5):\n",
    "    # describe the base function sin(x)/x\n",
    "    Wx = 0\n",
    "    if np.abs(x)<=1:\n",
    "        Wx = (a+2)*(np.abs(x)**3) - (a+3)*x**2 + 1\n",
    "    elif 1<=np.abs(x)<=2:\n",
    "        Wx = a*(np.abs(x)**3) - 5*a*(np.abs(x)**2) + 8*a*np.abs(x) - 4*a\n",
    "    return Wx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "b8692293-401d-421c-8be4-f52159fe51fc"
   },
   "source": [
    "### 辅助函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "uuid": "4966fadd-c619-4cca-b51b-c831def79f70"
   },
   "outputs": [],
   "source": [
    "def padding(img):\n",
    "    h, w, c = img.shape\n",
    "    print(img.shape)\n",
    "    pad_image = np.zeros((h+4, w+4, c))\n",
    "    pad_image[2:h+2, 2:w+2] = img\n",
    "    return pad_image"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "8b78f8df-bc24-47cb-a2ff-2768040f2c95"
   },
   "source": [
    "### BiCubic插值函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "uuid": "a45f945c-085d-428d-82ff-c2649e98bdcb"
   },
   "outputs": [],
   "source": [
    "def bicubic(img, sacle, a=-0.5):\n",
    "    print(\"Doing bicubic\")\n",
    "    h, w, color = img.shape\n",
    "    img = padding(img)\n",
    "    nh = h*sacle\n",
    "    nw = h*sacle\n",
    "    new_img = np.zeros((nh, nw, color))\n",
    "\n",
    "    for c in range(color):\n",
    "        for i in range(nw):\n",
    "            for j in range(nh):\n",
    "\n",
    "                px = i/sacle + 2\n",
    "                py = j/sacle + 2\n",
    "                px_int = int(px)\n",
    "                py_int = int(py)\n",
    "                u = px - px_int\n",
    "                v = py - py_int\n",
    "\n",
    "                A = np.matrix([[base_function(u+1, a)], [base_function(u, a)], [base_function(u-1, a)], [base_function(u-2, a)]])\n",
    "                C = np.matrix([base_function(v+1, a), base_function(v, a), base_function(v-1, a), base_function(v-2, a)])\n",
    "                B = np.matrix([[img[py_int-1, px_int-1][c], img[py_int-1, px_int][c], img[py_int-1, px_int+1][c], img[py_int-1, px_int+2][c]],\n",
    "                               [img[py_int, px_int-1][c], img[py_int, px_int][c], img[py_int, px_int+1][c], img[py_int, px_int+2][c]],\n",
    "                               [img[py_int+1, px_int-1][c], img[py_int+1, px_int][c], img[py_int+1, px_int+1][c], img[py_int+1, px_int+2][c]],\n",
    "                               [img[py_int+2, px_int-1][c], img[py_int+2, px_int][c], img[py_int+2, px_int+1][c], img[py_int+2, px_int+2][c]]])\n",
    "                new_img[j, i][c] = np.dot(np.dot(C, B), A)\n",
    "    return new_img"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "f75ca4ea-3824-43f5-ba36-9495300f25d3"
   },
   "source": [
    "### 使用BiCubic插值函数进行增强"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "uuid": "8afaffdc-cf1b-4a78-b48c-6b9bc316a9fd"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[38 38 29]\n",
      "  [39 39 30]\n",
      "  [40 37 31]\n",
      "  ...\n",
      "  [23 20 14]\n",
      "  [23 20 14]\n",
      "  [23 20 14]]\n",
      "\n",
      " [[36 36 27]\n",
      "  [38 38 29]\n",
      "  [41 38 32]\n",
      "  ...\n",
      "  [24 21 15]\n",
      "  [23 20 14]\n",
      "  [23 20 14]]\n",
      "\n",
      " [[40 38 29]\n",
      "  [40 38 29]\n",
      "  [42 40 31]\n",
      "  ...\n",
      "  [22 21 15]\n",
      "  [22 22 13]\n",
      "  [22 22 13]]\n",
      "\n",
      " ...\n",
      "\n",
      " [[20  7  3]\n",
      "  [20  7  3]\n",
      "  [19  9  4]\n",
      "  ...\n",
      "  [81  1  1]\n",
      "  [78  3  2]\n",
      "  [76  1  0]]\n",
      "\n",
      " [[16  6  1]\n",
      "  [16  6  1]\n",
      "  [16  6  1]\n",
      "  ...\n",
      "  [77  2  1]\n",
      "  [75  2  1]\n",
      "  [74  1  0]]\n",
      "\n",
      " [[18  8  3]\n",
      "  [18  8  3]\n",
      "  [16  6  1]\n",
      "  ...\n",
      "  [76  1  0]\n",
      "  [74  1  0]\n",
      "  [74  1  0]]]\n",
      "Doing bicubic\n",
      "(270, 480, 3)\n",
      "[[[ 38.          38.          29.        ]\n",
      "  [ 40.8515625   40.921875    31.21875   ]\n",
      "  [ 40.8125      41.          31.25      ]\n",
      "  ...\n",
      "  [183.28125    169.484375   176.890625  ]\n",
      "  [182.375      168.875      176.875     ]\n",
      "  [181.28125    168.078125   176.671875  ]]\n",
      "\n",
      " [[ 40.171875    40.21875     30.5859375 ]\n",
      "  [ 43.22949219  43.35656738  32.96954346]\n",
      "  [ 43.24902344  43.50390625  33.06396484]\n",
      "  ...\n",
      "  [195.94525146 181.15045166 189.07745361]\n",
      "  [194.92724609 180.47802734 189.04052734]\n",
      "  [193.70928955 179.60565186 188.8036499 ]]\n",
      "\n",
      " [[ 39.125       39.25        29.6875    ]\n",
      "  [ 42.16894531  42.38183594  32.06787109]\n",
      "  [ 42.28125     42.625       32.25390625]\n",
      "  ...\n",
      "  [194.19580078 179.46240234 187.33154297]\n",
      "  [193.11328125 178.76953125 187.26953125]\n",
      "  [191.83349609 177.87939453 187.01025391]]\n",
      "\n",
      " ...\n",
      "\n",
      " [[ 14.484375     6.515625     2.53125   ]\n",
      "  [ 15.54345703   7.0144043    2.74987793]\n",
      "  [ 15.49804688   7.03125      2.79785156]\n",
      "  ...\n",
      "  [ 37.22607422  30.36547852  21.28857422]\n",
      "  [ 37.57324219  30.00292969  21.63574219]\n",
      "  [ 37.89550781  29.61547852  21.95800781]]\n",
      "\n",
      " [[  9.125        4.125        1.625     ]\n",
      "  [  9.79296875   4.44140625   1.765625  ]\n",
      "  [  9.765625     4.453125     1.796875  ]\n",
      "  ...\n",
      "  [ 23.38769531  19.08300781  13.38769531]\n",
      "  [ 23.59375     18.84375     13.59375   ]\n",
      "  [ 23.78417969  18.58886719  13.78417969]]\n",
      "\n",
      " [[  3.703125     1.671875     0.65625   ]\n",
      "  [  3.97412109   1.80004883   0.7130127 ]\n",
      "  [  3.96289062   1.8046875    0.72558594]\n",
      "  ...\n",
      "  [  9.49804688   7.74926758   5.43554688]\n",
      "  [  9.58300781   7.65332031   5.52050781]\n",
      "  [  9.66162109   7.55102539   5.59912109]]]\n",
      "Finish\n"
     ]
    }
   ],
   "source": [
    "sacle = 4\n",
    "path = \"./l/Youku_00000_l/001.bmp\"\n",
    "img = cv2.imread(path)\n",
    "print(img)\n",
    "new_img = bicubic(img, sacle)\n",
    "print(new_img)\n",
    "cv2.imwrite( \"./008_l_bicubic.png\", new_img)\n",
    "print(\"Finish\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "uuid": "bf600a4a-1292-40d8-9fa3-4a5c37b2f04c"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "tianchi_metadata": {
   "competitions": [],
   "datasets": [],
   "description": "",
   "notebookId": "60365",
   "source": "ailab"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
